package com.ihr360.binlog.health;

import com.ihr360.binlog.component.BinlogReceiverStarter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class HealthCheck implements HealthIndicator {
    @Autowired
    private BinlogReceiverStarter binlogReceiverStarter;

    @Override
    public Health health() {
        HealthInfo info = check(); // perform some specific health check
        if (info.getCode() != 0) {
            return Health.down()
                    .withDetail("Error Code", info).build();
        }
        return Health.up().build();
    }

    public HealthInfo check() {
        if (binlogReceiverStarter.getOnError()) {
            return new HealthInfo(-1, binlogReceiverStarter.getErrorMessage());
        }
        return HealthInfo.noError;
    }
}